﻿using CinemaMaestro.BusinessLayer.Contracts;
using CinemaMaestro.Common.IocContainer;
using CinemaMaestro.DataLayer.Contracts.Entities;
using CinemaMaestro.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace CinemaMaestro.RestApi.Controllers
{
    [Authorize]
    public class SalesController : ApiController
    {

        [HttpPost]
        public IHttpActionResult CreateSale(int id, SaleViewModel saleVM)
        {
            var businessUoW = Resolver.Get<IBusinessObjectFactory>();
            Sale sale = saleVM;
            var success = businessUoW.Sales.Create(sale, id);
            return Ok(success);
        }

        [HttpGet]
        [Route("api/availableSeatCount/{screeningId}")]
        public IHttpActionResult GetAvailableSeatCount(int screeningId)
        {
            var businessUoW = Resolver.Get<IBusinessObjectFactory>();

            var screening = businessUoW.Screenings.Read().FirstOrDefault(x => x.Id == screeningId);
            if (screening == null)
            {
                return NotFound();
            }
            var capacity = screening.Room.Capacity;

            var reservedSeatCount = businessUoW.Reservations.Read()
                .Where(x => x.Screening.Id == screeningId)
                .Sum(x => x.TicketNr + x.DiscountTicketNr);

            var soldSeatCount = businessUoW.Sales.Read()
                .Where(x => x.Screening.Id == screeningId)
                .Sum(x => x.TicketNr + x.DiscountTicketNr);

            var availableSeatCount = capacity - soldSeatCount - reservedSeatCount;

            return Ok(availableSeatCount);
        }

        [HttpGet]
        [Route("api/occupiedSeats/{screeningId}")]
        public IHttpActionResult GetOccupiedSeats(int screeningId)
        {
            var businessUoW = Resolver.Get<IBusinessObjectFactory>();

            var screening = businessUoW.Screenings.Read().FirstOrDefault(x => x.Id == screeningId);
            if (screening == null)
            {
                return NotFound();
            }
            string occupiedSeats = String.Empty;

            var reservations = businessUoW.Reservations.Read()
                .Where(x => x.Screening.Id == screeningId);
            foreach (var reservation in reservations)
            {
                occupiedSeats += reservation.ReservatedSeats;  //1_2,1_3,...
                occupiedSeats += ",";  
            }

            var sales = businessUoW.Sales.Read()
                .Where(x => x.Screening.Id == screeningId);
            foreach (var sale in sales)
            {
                occupiedSeats += sale.Seats;  //1_2,1_3,...
                occupiedSeats += ",";
            }
            if (occupiedSeats != String.Empty)
            {
                occupiedSeats = occupiedSeats.Substring(0, occupiedSeats.Length - 1);
            }   

            return Ok(occupiedSeats);
        }
    }
}
